package br.edu.universo.si.projeto2.infojob.client.componentes;

import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * COMPONENTE - Campo para CEP
 * @author Andrey A. de Oliveira
 * @since 21/09/2008
 */
public class CEPBox extends TextBox
{
	
	public CEPBox()
	{
		
		super();
		this.setMaxLength(10);
		this.setWidth("100px");
		this.setTextAlignment(ALIGN_RIGHT);
		
		this.addKeyboardListener(
			new KeyboardListener()
			{
				public void onKeyDown(Widget sender, char keyCode, int modifiers)
				{
					if (keyCode != '0' && keyCode != '1' && keyCode != '2' && keyCode != '3' && keyCode != '4' && 
						keyCode != '5' && keyCode != '6' && keyCode != '7' && keyCode != '8' && keyCode != '9' &&
						((int) keyCode) != KEY_BACKSPACE && ((int) keyCode) != KEY_DELETE && ((int) keyCode) != KEY_HOME && ((int) keyCode) != KEY_END 
						&& ((int) keyCode) != KEY_TAB && ((int) keyCode) != KEY_LEFT && ((int) keyCode) != KEY_RIGHT && ((int) keyCode) != KEY_SHIFT)
					{
						((TextBox) sender).cancelKey();
					}
				}
				
				public void onKeyPress(Widget sender, char keyCode, int modifiers)
				{
					if (keyCode != (char) KEY_BACKSPACE)
					{
						if (CEPBox.this.getText().length() == 2)
						{
							CEPBox.this.setText(CEPBox.this.getText() + ".");
						}
						
						if (CEPBox.this.getText().length() == 6)
						{
							CEPBox.this.setText(CEPBox.this.getText() + "-");
						}
						
					}
				};
				
				// implementacao vazia...
				public void onKeyUp(Widget sender, char keyCode, int modifiers) { };
			}
		);
		
		// limpa o campo caso o CNPJ nao esteja completo
		this.addFocusListener(
			new FocusListener()
			{
				public void onFocus(Widget sender) { };

				public void onLostFocus(Widget sender)
				{
					if (CEPBox.this.getText().length() != 10)
					{
						CEPBox.this.setText("");
					}
				};
			}
		);
	}

}
